import django_filters
from .models import GasStationInfo, OilInfo, Orders, ApiChannel
from core.tools.func_utils import get_lng_lat_tuple
from django.db.models import Q


# 加油站筛选
class GasStationInfoFilter(django_filters.FilterSet):
    orderBy = ((0, "默认不排序"), (1, "距离"), (2, "价格"))
    gasType = django_filters.ChoiceFilter(help_text="类型", choices=GasStationInfo.gas_type, label="类型")
    oilNo = django_filters.NumberFilter(method="filter_oil_No", help_text="油号", label="油号")
    position = django_filters.CharFilter(method="filter_position", label="经纬度xx,xx")
    dis = django_filters.CharFilter(method="filter_dis", label="范围")
    order_by = django_filters.ChoiceFilter(method="filter_order_by", label="排序", choices=orderBy)
    mobile = django_filters.CharFilter(method="filter_mobile", label="手机号")

    def filter_mobile(self, queryset, name, value):
        return queryset

    def filter_order_by(self, queryset, name, value):
        return queryset

    def filter_dis(self, queryset, name, value):
        # position = self.request.query_params.get("position")
        return queryset

    def filter_position(self, queryset, name, value):
        value = value.split(",")
        if self.request:
            dis = self.request.query_params.get("dis")
        else:
            dis = 20
        if int(dis) == 10:
            dis = 8
        elif int(dis) == 15:
            dis = 14
        elif int(dis) == 20:
            dis = 16
        top, bottom, left, right = get_lng_lat_tuple(*value, dis=int(dis))
        queryset = queryset.filter(Q(gasAddressLatitude__lte=top[1], gasAddressLatitude__gte=bottom[1]),
                                   Q(gasAddressLongitude__gte=left[0], gasAddressLongitude__lte=right[0]))
        return queryset

    def filter_oil_No(self, queryset, name, value):
        queryset = queryset.filter(id__in=OilInfo.objects.filter(oilNo=value).values_list("gs_station_id"))
        return queryset

    class Meta:
        model = GasStationInfo
        fields = ("gasType", "oilNo", "position", "dis", "order_by")


# 订单筛选
class OrdersFilter(django_filters.FilterSet):
    result = django_filters.ChoiceFilter(help_text="支付结果", method="filter_result", choices=Orders.orderStatus,
                                         label="支付结果")

    def filter_result(self, queryset, name, value):
        if value == 2 or value == "2":
            return queryset.filter(result__in=[2, 6])
        else:
            return queryset.filter(result=value)

    class Meta:
        model = Orders
        fields = ("result",)


# 后台订单筛选
class BaseAdminOrdersFilter(django_filters.FilterSet):
    channel = django_filters.ChoiceFilter(help_text="渠道", method="filter_channel",
                                          choices=ApiChannel.objects.all().values_list("id", "name"))
    start_time = django_filters.DateTimeFilter(help_text="开始时间", method="filter_start_time", label="开始时间")
    end_time = django_filters.DateTimeFilter(help_text="结束时间", method="filter_end_time", label="结束时间")

    def filter_channel(self, queryset, name, value):
        return queryset.filter(channel__id=value)

    def filter_end_time(self, queryset, name, value):
        return queryset.filter(Q(create_time__lt=value) | Q(refund__create_time__lt=value))

    def filter_start_time(self, queryset, name, value):
        return queryset.filter(Q(create_time__gte=value) | Q(refund__create_time__gte=value))

    class Meta:
        model = Orders
        fields = ("channel", "start_time", "end_time")


# 后台订单筛选
class AdminOrdersFilter(BaseAdminOrdersFilter):
    result = django_filters.ChoiceFilter(help_text="支付结果", method="filter_result", choices=Orders.orderStatus,
                                         label="支付结果")

    def filter_result(self, queryset, name, value):
        if value == 1:
            return queryset.exclude(result__in=[0, 101, 2])
        return queryset.filter(result=value)

    class Meta:
        model = Orders
        fields = ("result", "channel", "start_time", "end_time")


# 后台订单结算
class AdminOrdersTotalFilter(django_filters.FilterSet):
    channel = django_filters.ChoiceFilter(help_text="渠道", method="filter_channel",
                                          choices=ApiChannel.objects.all().values_list("id", "name"))
    start_time = django_filters.DateTimeFilter(help_text="开始时间", method="filter_start_time", label="开始时间")
    end_time = django_filters.DateTimeFilter(help_text="结束时间", method="filter_end_time", label="结束时间")

    def filter_channel(self, queryset, name, value):
        return queryset.filter(channel__id=value)

    def filter_end_time(self, queryset, name, value):
        return queryset.filter(Q(create_time__lt=value) | Q(refund__create_time__lt=value))

    def filter_start_time(self, queryset, name, value):
        return queryset.filter(Q(create_time__gte=value) | Q(refund__create_time__gte=value))

    class Meta:
        model = Orders
        fields = ("channel", "start_time", "end_time")
